﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using uMES.LeanManufacturing.Common;
using uMES.LeanManufacturing.ParameterDTO;
using uMES.LeanManufacturing.ReportBusiness;
using uMES.LeanManufacturing.DBUtility;

public partial class Report_BwContainerWorkHoursForm : PageBase
{
    int m_PageSize = 50;
    uMESReportBusiness reportBal = new uMESReportBusiness();
    protected void Page_Load(object sender, EventArgs e)
    {
        uMESMasterPage master = this.Master as uMESMasterPage;
        master.strNavigation = "当前位置：工人工时记录报表";
        master.strTitle = "工人工时记录报表";
        master.ChangeFrame(true);
        upageTurning.PageIndexChanged += new pageTurning.PageIndexChangedEventHandler(() => { SearchData(upageTurning.CurrentPageIndex.ToString()); });
        WebPanel = WebAsyncRefreshPanel1;
        ClearMessage();

        if (IsPostBack == false) {
            DataTable dt = OracleHelper.QueryDataByEntity(new ExcuteEntity("factory", ExcuteType.selectAll) {
                strWhere = " and description is not null "
            });
            ddlFactory.DataValueField = "FactoryID";
            ddlFactory.DataTextField = "description";
            ddlFactory.DataSource = dt;
            ddlFactory.DataBind();
            ddlFactory.Items.Insert(0, "");

        }
    }

    protected void ddlFactory_TextChanged(object sender, EventArgs e)
    {
        DataTable dt = OracleHelper.QueryDataByEntity(new ExcuteEntity("Team", ExcuteType.selectAll)
        {
          WhereFileds=new List<FieldEntity>() { new FieldEntity("Factoryid",ddlFactory.SelectedValue,FieldType.Str)}
        });
        ddlTeam.DataValueField = "TeamID";
        ddlTeam.DataTextField = "TeamName";
        ddlTeam.DataSource = dt;
        ddlTeam.DataBind();
        ddlTeam.Items.Insert(0, "");
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {

        try {gdResult.Rows.Clear(); SearchData("1"); } catch (Exception ex) { DisplayMessage(ex.Message, false); }
    }
    protected void btnReSet_Click(object sender, EventArgs e)
    {
        txtContainerName.Text = "";txtProcessNo.Text = "";txtSpecName.Text = "";gdResult.Rows.Clear();
        txtAllWorkHors.Text = "";txtStartDate.Value = "";txtEndDate.Value = "";
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        try { ExportToExcel(); } catch (Exception ex) { DisplayMessage(ex.Message, false); }
    }
    void SearchData(string index)
    {
        txtAllWorkHors.Text = "";
        Dictionary<string, string> para = new Dictionary<string, string>();
        para["CurrentPageIndex"] = index.ToString();
        para["PageSize"] = m_PageSize.ToString();

        para["ProcessNo"] = txtProcessNo.Text.Trim();
        para["ContainerName"] = txtContainerName.Text.Trim();
        para["StepName"] = txtSpecName.Text.Trim();

        para["WorkerName"] = txtWorkerName.Text.Trim();
        para["TeamID"] = ddlTeam.SelectedValue;
        para["FactoryID"] = ddlFactory.SelectedValue;
        para["StartDate"] = txtStartDate.Value;
        para["EndtDate"] = txtEndDate.Value;

        uMESPagingDataDTO result = reportBal.GetReportWorkHoursInfo(para);

        this.gdResult.DataSource = result.DBTable;
        this.gdResult.DataBind();
            //给分页控件赋值，用于分页控件信息显示
            this.upageTurning.TotalRowCount = int.Parse(result.RowCount);
            this.upageTurning.RowCountByPage = m_PageSize;
        //赋值总工时
        if(result.DBTable.Rows.Count>0)
            txtAllWorkHors.Text = result.DBTable.Rows[0]["AllReportHours"].ToString();
    }

    DataTable SearchAllData( )
    {
        Dictionary<string, string> para = new Dictionary<string, string>();
        para["CurrentPageIndex"] ="all";
        para["PageSize"] = m_PageSize.ToString();

        para["ProcessNo"] = txtProcessNo.Text.Trim();
        para["ContainerName"] = txtContainerName.Text.Trim();
        para["StepName"] = txtSpecName.Text.Trim();

        para["WorkerName"] = txtWorkerName.Text.Trim();
        para["TeamID"] = ddlTeam.SelectedValue;
        para["FactoryID"] =ddlFactory.SelectedValue;
        para["StartDate"] = txtStartDate.Value;
        para["EndtDate"] = txtEndDate.Value;

        uMESPagingDataDTO result = reportBal.GetReportWorkHoursInfo(para);

        return result.DBTable;

    }

    void ExportToExcel() {

        // DataTable dt = uMESCommonFunction.GetGridData(gdResult);

        DataTable dt = SearchAllData();
        //取有效列，且重命名列名
        DataView view = dt.DefaultView;

        dt = view.ToTable(false, "ProcessNo", "ContainerName", "ProductDesc", "conqty", "workflowstepname", "ReportHours", "workerName", "fullname",
            "teamname", "factoryname", "reportdate", "reporttype");

        dt.Columns["ProcessNo"].ColumnName = "工作令号";
        dt.Columns["ContainerName"].ColumnName = "批次";
        dt.Columns["ProductDesc"].ColumnName = "产品名称";
        dt.Columns["workflowstepname"].ColumnName = "工序";

        if (dt.Columns.Contains("Qty"))
            dt.Columns["Qty"].ColumnName = "报工数量";

        dt.Columns["ConQty"].ColumnName = "批次数量";

        if(dt.Columns.Contains("unitworktime"))
            dt.Columns["unitworktime"].ColumnName = "单件工时";
        if (dt.Columns.Contains("setupworktime"))
            dt.Columns["setupworktime"].ColumnName = "准备工时";

        dt.Columns["ReportHours"].ColumnName = "报工工时";
        dt.Columns["workerName"].ColumnName = "协作工人";
        dt.Columns["fullname"].ColumnName = "报工人";
        dt.Columns["teamname"].ColumnName = "报工班组";
        dt.Columns["factoryname"].ColumnName = "报工分厂";
        dt.Columns["reportdate"].ColumnName = "报工时间";
        dt.Columns["reporttype"].ColumnName = "送检类型";

        string strPath = Request.MapPath(Request.ApplicationPath);
        string filePath = ExportHelper.DataTableExportToExcel2(dt, "工人工时记录报表", strPath);
        string url = Request.Url.ToString();
        string baseUrl = Regex.Split(url, "/Report")[0];
        string fileUrl = baseUrl + filePath;


        string strScript = "<script>window.open('" + fileUrl + "');</script>";

        ExcuteScript(strScript);
    }

   
}